package view;

import java.sql.SQLException;
import java.util.Scanner;

import model.Employee;
import service.EmployeeAPI;
import service.ProjectAPI;
import service.TimesheetAPI;
import service.implement.EmployeeDAO;
import service.implement.ProjectDAO;
import service.implement.TimesheetDAO;

public class Run {
	static Scanner sc=new Scanner(System.in);
	static EmployeeApp empapp=new EmployeeApp();
	static ProjectApp pjapp=new ProjectApp();
	static TimesheetApp tsapp=new TimesheetApp();
	public static void capNhat(EmployeeAPI dao,ProjectAPI pj,TimesheetAPI ts){
		Employee em=new Employee();
		int chon=1;
		System.out.println("1. Cap nhat Employee");
		System.out.println("2. Cap nhat Project");
		System.out.println("3. Cap nhat Timesheet");
		System.out.println("4.Thoat");
		int choise=Integer.parseInt(sc.nextLine());
		switch(choise){
		case 1:{
			empapp.menuCapnhatEmp();
			int chon2=Integer.parseInt(sc.nextLine());
			switch(chon2){
			case 1:
				empapp.capNhatEmpbyid();
				break;
			case 2:
				empapp.capNhatEmpbyname();
				break;
			case 3:
				empapp.capNhatbyUsername();
				break;
			case 4:break;
			default: System.out.println("Nhap 1-4");
			}
		}
		break;
		case 2:{
			pjapp.menuCapnhatPj();
			choise=Integer.parseInt(sc.nextLine());
			switch(choise){
			case 1:
				pjapp.capNhatpjbyid();
				break;
			case 2:
				pjapp.capNhatPjbyName();
				break;
			case 3:break;
			default:System.out.println("Nhap 1-3");
			}
		}
		break;
		case 3:{
			tsapp.menuCapnhatTs();
			choise=Integer.parseInt(sc.nextLine());
			switch(choise){
			case 1:
				tsapp.capNhattsbyid();
				break;
			case 2:
				tsapp.capNhattsbyEmpid();
				break;
			case 3:
				tsapp.capNhattsbyPjId();
				break;
			case 4:break;
			default: System.out.println("Nhap 1-4");
			}
		}
		break;
		case 4:break;
		default: System.out.println("Nhap 1-4");
		}
	}
	public static void inThongtin(EmployeeAPI dao,ProjectAPI pj,TimesheetAPI ts){
		Scanner sc=new Scanner(System.in);
		System.out.println("1. In danh sach Employee");
		System.out.println("2. In danh sach Project");
		System.out.println("3. In danh sach Timesheet");
		System.out.println("4.Thoat");
		int choise=Integer.parseInt(sc.nextLine());
		switch(choise){
		case 1:{
			empapp.inEmp();
			break;
		}
		case 2:
			pjapp.inPj();	
			break;
		case 3:
			tsapp.inTs();
			break;
		case 4:break;
		}
	}
	public static void nhapThongtin(EmployeeAPI dao, ProjectAPI pj, TimesheetAPI ts){
		try{
			Employee employee=new Employee();
			System.out.println("1.Nhap du lieu Employee");
			System.out.println("2.Nhap du lieu Project");
			System.out.println("3.Khai Timesheet");
			System.out.println("4.Thoat");
			int choise=Integer.parseInt(sc.nextLine());
			switch(choise){
			case 1:
				empapp.nhapEmp();
				break;
			case 2:
				pjapp.nhapPj();	
				break;
			case 3:{
				tsapp.nhapTs();
				break;
			}
			case 4:break;
			default:System.out.println("Nhap 1-3");}

		}catch(NumberFormatException e){
			System.out.println("Du lieu chua duoc nhap hoac nhap sai.Vui long thu lai!");
		}
	}
	public static void delete(EmployeeAPI emp,ProjectAPI pj,TimesheetAPI ts){
		try{
			System.out.println("1.Xoa Employee");
			System.out.println("2.Xoa Project");
			System.out.println("3.Xoa Timesheet");
			System.out.println("4.Thoat");
			int choise=Integer.parseInt(sc.nextLine());
			switch(choise){
			case 1:{
				empapp.menuDeleteEmp();
				int chon=Integer.parseInt(sc.nextLine());
				switch(chon){
				case 1:empapp.deleteEmpById();
				break;
				case 2:{
					empapp.deleteEmpbyName();
					break;
				}
				case 3:break;
				default: System.out.println("Nhap 1-3");
				}
				break;
			}
			case 2:{
				pjapp.menuDeletePj();
				int chon=Integer.parseInt(sc.nextLine());
				switch(chon){
				case 1:
					pjapp.deletePjbyId();
					break;
				case 2:
					pjapp.deletePjByName();
					break;
				case 3:break;
				default:System.out.println("Nhap 1-3");
				}
				break;
			}	
			case 3:
				tsapp.menuDeleteTs();
				int chon=Integer.parseInt(sc.nextLine());
				switch(chon){
				case 1:
					tsapp.deleteTsbyId();
					break;
				case 2:
					tsapp.deleteTsByEmpId();
					break;
				case 3:
					tsapp.deleteTsByPjId();
					break;
				case 4:
					return;
				default:System.out.println("Nhap 1-4");
				}
				break;
			case 4:break;
			default:System.out.println("Nhap 1-4");
			}
		}catch(NumberFormatException e){
			System.out.println("Du lieu chua duoc nhap hoac nhap sai.Vui long thu lai!");
		}
	}
	public static void timkiem(EmployeeAPI emp,ProjectAPI pj,TimesheetAPI ts){
		int chon=1;
		do{
			System.out.println("1.Tim Employee");
			System.out.println("2.Tim Project");
			System.out.println("3.Tim Timesheet");
			System.out.println("4.Thoat");
			try{
				chon=Integer.parseInt(sc.nextLine());	
				switch(chon){
				case 1:{
					empapp.menuTimkiemEmp();
					int choise=Integer.parseInt(sc.nextLine());
					switch(choise){
					case 1:
						empapp.searchEmp();
						return;
					case 2:
						empapp.searchByName();
						return;
					case 3:
						return;
					default:
						System.out.println("Nhap 1-3");
					}
				}
				break;
				case 2:{
					pjapp.menuTimkiemPj();
					int choise=Integer.parseInt(sc.nextLine());
					switch(choise){
					case 1:
						pjapp.searchPjbyId();
						return;

					case 2:
						pjapp.searchPjbyName();
						return;
					case 3:return;
					default:
						System.out.println("Nhap 1-3");	
					}
				}
				break;
				case 3:{
					tsapp.menuTimkiemTs();
					int choise=Integer.parseInt(sc.nextLine());
					switch(choise){
					case 1:
						tsapp.searchTsByid();
						return;

					case 2:
						tsapp.searchTsByEmpId();
						return;
					case 3:
						tsapp.searchTsByPjId();
						return;

					case 4:
						return;
					default: System.out.println("Nhap 1-3");
					}
				}
				break;
				case 4:return;
				default: System.out.println("Nhap 1-4");
				}
			}
			catch(NumberFormatException e){
				System.out.println("Nhap sai dinh dang hoac chua nhap.Vui long thu lai!");
			}
		}	
		while(chon!=0);
	}
	public static void main(String args[]) throws ClassNotFoundException, SQLException{
		EmployeeAPI dao=new EmployeeDAO();
		ProjectAPI pj=new ProjectDAO();
		TimesheetAPI ts=new TimesheetDAO();
		int chon=1;
		do{
			System.out.println("----------Menu-----------");
			System.out.println("1.Nhap du lieu");
			System.out.println("2.Xoa thong tin");
			System.out.println("3.Cap nhat");
			System.out.println("4.In thong tin");
			System.out.println("5.Tim kiem");
			System.out.println("6.Thoat");
			try{
				chon=Integer.parseInt(sc.nextLine());	
				switch(chon){
				case 1: nhapThongtin(dao, pj,ts);
				break;
				case 2:	delete(dao, pj, ts);
				break;		
				case 3: capNhat(dao, pj, ts);
				break;
				case 4:inThongtin(dao, pj, ts);
				break;
				case 5:timkiem(dao, pj, ts);
				break;
				case 6:return;
				default:System.out.println("Nhap 1-6");
				}
			}
			catch(NumberFormatException e){
				System.out.println("Nhap sai dinh dang hoac chua nhap.Vui long thu lai!");
			}
		}
		while(chon!=0);
	}
}